package com.ruoyi.system.service.impl;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.VideoUtil;
import com.ruoyi.system.domain.DzKecheng;
import com.ruoyi.system.service.IDzKechengService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzZhangjieMapper;
import com.ruoyi.system.domain.DzZhangjie;
import com.ruoyi.system.service.IDzZhangjieService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 章节信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-01
 */
@Service
public class DzZhangjieServiceImpl implements IDzZhangjieService 
{
    @Autowired
    private DzZhangjieMapper dzZhangjieMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzKechengService dzKechengService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询章节信息
     * 
     * @param zjId 章节信息主键
     * @return 章节信息
     */
    @Override
    public DzZhangjie selectDzZhangjieByZjId(Long zjId)
    {
        return dzZhangjieMapper.selectDzZhangjieByZjId(zjId);
    }

    /**
     * 查询章节信息列表
     * 
     * @param dzZhangjie 章节信息
     * @return 章节信息
     */
    @Override
    public List<DzZhangjie> selectDzZhangjieList(DzZhangjie dzZhangjie)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzZhangjie.setDatascope(datascope);
        if(datascope==4){
            dzZhangjie.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzZhangjie.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzZhangjie.setCheckHuiyuanId(0l);
            }

        }
        return dzZhangjieMapper.selectDzZhangjieList(dzZhangjie);
    }

    @Override
    public List<DzZhangjie> selectDzZhangjieListWeb(DzZhangjie dzZhangjie)
    {
        return dzZhangjieMapper.selectDzZhangjieList(dzZhangjie);
    }

    /**
     * 新增章节信息
     * 
     * @param dzZhangjie 章节信息
     * @return 结果
     */
    @Override
    public int insertDzZhangjie(DzZhangjie dzZhangjie)
    {
        dzZhangjie.setCreateTime(DateUtils.getNowDate());
        //获取章节时长（秒）
        String videoPath = RuoYiConfig.getUploadPath() + dzZhangjie.getVideoFile().replace("/profile/upload","");
        try {
            System.out.println("视频地址："+videoPath);

            System.out.println("视频时长："+VideoUtil.getMp4Duration(videoPath));
            dzZhangjie.setVideoTime(VideoUtil.getMp4Duration(videoPath)+"");
        } catch (IOException e) {

        }
        dzZhangjieMapper.insertDzZhangjie(dzZhangjie);
        DzZhangjie query = new DzZhangjie();
        query.setKcId(dzZhangjie.getKcId());
        //遍历所有章节，获取总时长，变更视频总时长
        List<DzZhangjie> zllist = dzZhangjieMapper.selectDzZhangjieList(query);
        long total = 0l;
        for(DzZhangjie temp:zllist){
            total+=Long.parseLong(temp.getVideoTime());
        }
        DzKecheng kecheng =dzKechengService.selectDzKechengByKcId(dzZhangjie.getKcId());
        kecheng.setKctotal(total);
        dzKechengService.updateDzKecheng(kecheng);

        return 1;
    }

    /**
     * 修改章节信息
     * 
     * @param dzZhangjie 章节信息
     * @return 结果
     */
    @Override
    public int updateDzZhangjie(DzZhangjie dzZhangjie)
    {
        //获取章节时长（秒）
        String videoPath = RuoYiConfig.getUploadPath() + dzZhangjie.getVideoFile().replace("/profile/upload","");
        System.out.println("@@@@@@@@@dzZhangjie.getVideoFile().replace()章节:"+dzZhangjie.getVideoFile().replace("/profile/upload",""));
        System.out.println("@@@@@@@@@RuoYiConfig.getUploadPath()章节:"+RuoYiConfig.getUploadPath());
        System.out.println("@@@@@@@@@videoPath章节:"+videoPath);
        try {
            System.out.println("@@@@@@@@@章节:"+videoPath);
            System.out.println("@@@@@@@@@章节时长:"+VideoUtil.getMp4Duration(videoPath));
            dzZhangjie.setVideoTime(VideoUtil.getMp4Duration(videoPath)+"");
        } catch (IOException e) {
            e.printStackTrace();
        }

        dzZhangjieMapper.updateDzZhangjie(dzZhangjie);
        DzZhangjie query = new DzZhangjie();
        query.setKcId(dzZhangjie.getKcId());
        //遍历所有章节，获取总时长，变更视频总时长
        List<DzZhangjie> zllist = dzZhangjieMapper.selectDzZhangjieList(query);
        long total = 0l;
        for(DzZhangjie temp:zllist){
            total+=Long.parseLong(temp.getVideoTime());
        }
        DzKecheng kecheng =dzKechengService.selectDzKechengByKcId(dzZhangjie.getKcId());
        kecheng.setKctotal(total);
        dzKechengService.updateDzKecheng(kecheng);
        return 1;
    }

    /**
     * 批量删除章节信息
     * 
     * @param zjIds 需要删除的章节信息主键
     * @return 结果
     */
    @Override
    public int deleteDzZhangjieByZjIds(Long[] zjIds)
    {
        return dzZhangjieMapper.deleteDzZhangjieByZjIds(zjIds);
    }

    /**
     * 删除章节信息信息
     * 
     * @param zjId 章节信息主键
     * @return 结果
     */
    @Override
    public int deleteDzZhangjieByZjId(Long zjId)
    {
        return dzZhangjieMapper.deleteDzZhangjieByZjId(zjId);
    }
}
